<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px" style="display: flex; justify-content: flex-end;">
      <el-form-item label="姓名" prop="elderName">
        <el-input
          v-model="queryParams.elderName"
          placeholder="请输入老人姓名"
          size="mini"
          style="width: 120px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建日期" prop="dateTime">
        <el-date-picker
          v-model="queryParams.dateTime"
          type="datetimerange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 300px">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="5" class="mb8">
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
      <el-divider content-position="left"><span style="color: #999; font-size: 10px;">住户购买服务列表</span></el-divider>
    </el-row>

    <el-table v-loading="loading" :data="serviceList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="姓名" align="center" prop="elderName" />
      <el-table-column label="护理项目" align="center" prop="serviceName" />
      <el-table-column label="服务时长" align="center" prop="duration" />
      <el-table-column label="状态" align="center" prop="isFinish" >
        <template slot-scope="scope">
          <el-tag  v-if="scope.row.isFinish === 0" type="danger">未完成</el-tag>
          <el-tag v-if="scope.row.isFinish === 1" type="success">已完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-more"
            @click="handleUpdate(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleIsFinish(scope.row)"
          >服务完成</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改老人购买服务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="elderName">
          {{form.elderName}}
        </el-form-item>
        <el-form-item label="护理项目" prop="nurseName">
          <el-input v-model="form.serviceName" placeholder="请输入护理" />
        </el-form-item>
        <el-form-item label="项目详情" prop="nurseDescribe">
          {{form.nurseDescribe}}
        </el-form-item>
        <el-form-item label="服务时长" prop="duration">
          {{form.duration}}
        </el-form-item>
        <el-form-item label="服务价格" prop="nursePrice">
          {{form.nursePrice}}
        </el-form-item>
        <el-form-item label="服务备注" prop="nurseNotes">
          {{form.nurseNotes}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listService,
    getService,
    delService,
    addService,
    updateService,
    updateServiceIsFinish
  } from "@/api/business/elderService";

  export default {
    name: "Service",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 老人购买服务表格数据
        serviceList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          nurseName: null,
          elderName: null,
          dateTime:null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询老人购买服务列表 */
      getList() {
        this.loading = true;
        if(this.queryParams.dateTime != null) {
          const time = this.queryParams.dateTime;
          this.queryParams.startTime = time[0];
          this.queryParams.endTime = time[1]
        }
        listService(this.queryParams).then(response => {
          this.serviceList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          nurseId: null,
          elderId: null,
          duration: null,
          createTime: null,
          createUserId: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加老人购买服务";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getService(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改老人购买服务";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateService(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addService(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除老人购买服务编号为"' + ids + '"的数据项？').then(function() {
          return delService(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      handleIsFinish(row){
        const data = {
          serviceId : row.id,
          status : 'YES',
        }
        if(row.isFinish == 1){
          this.$modal.msgWarning("服务已完成，勿重复提交");
        }else {
          this.$modal.confirm('是否确认完成该老人购买服务？').then(function() {
            return updateServiceIsFinish(data);
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("服务完成");
          }).catch(() => {});
        }

      },
    }
  };
</script>
